package com.rainbow.bam.service.resource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rainbow.bam.dao.HtSourceRepository;
import com.rainbow.bam.dao.HtSrcActionRepository;
import com.rainbow.bam.entity.HtAccount;
import com.rainbow.bam.entity.HtSource;
import com.rainbow.bam.entity.HtSrcAction;
import com.rainbow.bam.entity.PaginationRequest;
import com.rainbow.bam.entity.PaginationResponse;

/**
 * 
 * 菜单管理服务实现类
 * @author zhaolei
 * @version $Id: ResourceServiceImpl.java, v 0.1 2015年11月12日 下午6:05:11 zhaolei Exp $
 */
@Service
public class HtSourceServiceImpl implements HtSourceService {

    @Autowired
    private HtSourceRepository htSourceRepository;
    
    @Autowired
    private HtSrcActionRepository htSrcActionRepository;

    /**
     * 
     * @see com.rainbow.bam.service.resource.HtSourceService#findAccountsByPagination(com.rainbow.bam.entity.PaginationRequest)
     */
    public PaginationResponse<HtSource> findAccountsByPagination(PaginationRequest paginationRequest) {
        return htSourceRepository.findResultsByPagination(paginationRequest);
    }
    
    /**
     * 
     * @see com.rainbow.bam.service.resource.HtSourceService#initAccountDefaultAuthority(com.rainbow.bam.entity.HtAccount)
     */
    public Set<String> initAccountDefaultAuthority(final HtAccount account) {
        String defaultActionSql = "select distinct t from HtSrcAction "
                + " t,HtDutyAction d,HtAccountDuty a "
                + " where t.id=d.htSrcAction.id "
                + " and d.htDuty.id=a.htDuty.id " + " and a.htAccount.id="+account.getId()
                + " and d.htDuty.status = 'A'" + " order by t.actionOrder asc";
        List<HtSrcAction> permitActionList =  htSrcActionRepository.findListByHql(defaultActionSql);
        Set<String> permissions = new HashSet<String>();
        
        for (HtSrcAction htSrcAction : permitActionList) {
            String methodName = htSrcAction.getMethodName();
            String[] methods = htSrcAction.getActionMethods().split(";");
            for (String method : methods) {
                if (method.trim().length() > 0) {
                    String actionKey = methodName +":"+ method ;
                    permissions.add(actionKey);
                }
            }
        }
         return permissions ;
    }
}
